package com.ft.oa.service.development.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ft.oa.domain.development.ProductCompetitor;
import com.ft.oa.domain.dto.sys.SecurityUser;
import com.ft.oa.enums.ManagerEnum;
import com.ft.oa.mapper.development.ProductCompetitorMapper;
import com.ft.oa.service.development.ProductCompetitorService;
import com.ft.oa.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class ProductCompetitorServiceImpl implements ProductCompetitorService {

    @Resource
    private ProductCompetitorMapper productCompetitorMapper;


    @Override
    public int add(ProductCompetitor productCompetitor) {

        SecurityUser currentUser = SecurityUtils.getCurrentUser();

        productCompetitor.setSubmitId(currentUser.getId());
        productCompetitor.setSubmit(currentUser.getUsername());
        productCompetitor.setCreateTime(LocalDateTime.now());

      return productCompetitorMapper.insert(productCompetitor);
    }


    @Override
    @Transactional
    public void update(ProductCompetitor competitor) {

        LambdaUpdateChainWrapper<ProductCompetitor> wrapper = new LambdaUpdateChainWrapper<>(productCompetitorMapper);
        boolean update = wrapper.eq(ProductCompetitor::getId, competitor.getId())
                .set(StringUtils.hasText(competitor.getCompanyName()), ProductCompetitor::getCompanyName, competitor.getCompanyName())
                .set(StringUtils.hasText(competitor.getCompetitorName()), ProductCompetitor::getCompetitorName, competitor.getCompetitorName())
                .set(StringUtils.hasText(competitor.getCompetitorVersion()), ProductCompetitor::getCompetitorVersion, competitor.getCompetitorVersion())
                .set(StringUtils.hasText(competitor.getSelfVersion()), ProductCompetitor::getSelfVersion, competitor.getSelfVersion())
                .set(StringUtils.hasText(competitor.getCompetitorDescribe()), ProductCompetitor::getCompetitorDescribe, competitor.getCompetitorDescribe())
                .set(StringUtils.hasText(competitor.getAdvantageDescribe()), ProductCompetitor::getAdvantageDescribe, competitor.getAdvantageDescribe())
                .set(StringUtils.hasText(competitor.getShortageDescribe()), ProductCompetitor::getShortageDescribe, competitor.getShortageDescribe())
                .set(StringUtils.hasText(competitor.getSuggest()), ProductCompetitor::getSuggest, competitor.getSuggest())
                .set(ProductCompetitor::getUpdateTime, LocalDateTime.now())
                .update();

    }


    @Override
    @Transactional
    public void del(Integer id) {

        LambdaUpdateChainWrapper<ProductCompetitor> wrapper = new LambdaUpdateChainWrapper<>(productCompetitorMapper);

        boolean update = wrapper.eq(ProductCompetitor::getId, id)
                .set(ProductCompetitor::getState, 1)
                .update();
    }


    @Override
    public Page<ProductCompetitor> page(ProductCompetitor competitor) {

        Page<ProductCompetitor> pages = new Page<>(competitor.getPage(), competitor.getSize());
        QueryWrapper<ProductCompetitor> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(ManagerEnum.GENERAL.toString().equals(SecurityUtils.getCurrentUser().getManager()), ProductCompetitor::getSubmitId ,SecurityUtils.getCurrentUser().getId())
                .eq(ProductCompetitor::getProductId, competitor.getProductId())
                .eq(ProductCompetitor::getState, 0)
                .like(StringUtils.hasText(competitor.getCompetitorName()),ProductCompetitor::getCompetitorName, competitor.getCompetitorName());
        Page<ProductCompetitor> page = productCompetitorMapper.selectPage(pages, queryWrapper);
        return page;
    }
}
